package com.jzh.selfwallpaper.view.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jzh.selfwallpaper.R;
import com.jzh.selfwallpaper.entity.ActiveListItem;
import com.jzh.selfwallpaper.view.adapter.commonrecyclerview.BaseRecyclerHolder;

import java.util.List;

import imageloader.libin.com.images.loader.ImageLoader;

public class ActiveFragmentAdapter extends RecyclerView.Adapter<ActiveFragmentAdapter.ViewHolder> {

    private List<ActiveListItem> list;
    private Context context;

    public OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public static interface OnItemClickListener {
        void onItemClick(View view,int position);

        void onItemLongClick(View view);
    }

    public ActiveFragmentAdapter(List<ActiveListItem> list, Context context) {
        this.list = list;
        this.context = context;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_active_item_bg;
        TextView tv_active_item_title;

        public ViewHolder(View itemView) {
            super(itemView);
            iv_active_item_bg = itemView.findViewById(R.id.iv_active_item_bg);
            tv_active_item_title = itemView.findViewById(R.id.tv_active_item_title);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_active_rv, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder,final int position) {
        ActiveListItem activeListItem = list.get(position);
        if (activeListItem == null) {
            return;
        }
        holder.tv_active_item_title.setText(activeListItem.title);
        ImageLoader.with(context).url(activeListItem.img_url).into(holder.iv_active_item_bg);
        holder.iv_active_item_bg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(v,position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() : 0;
    }

}
